<?php

define("LIBERATIONSWEB", 20091223);

session_start();

//Librairie de fonctions
require_once (__DIR__ . '/vendor/autoload.php');
require_once("lib/libconfig.inc.php");
require_once("lib/libsession.inc.php");
require_once("lib/libfonctions.inc.php");
require_once("lib/libmysql.inc.php");
require_once("lib/libencode.inc.php");
require_once("lib/libcourriel.inc.php");
require_once("lib/libliberations.inc.php");
require_once("lib/libcentrale.inc.php");
require_once("fpdf/fpdf.php");

class PDF extends FPDF {

    var $NomEmploye;
    var $NumEmploye;
    var $Adresse1;
    var $Adresse2;
    var $Ville;
    var $CodePostal;
    var $NumeroTel;
    var $Texte17;
    var $Texte18;
    var $Nomemployeur;
    var $configCentrale;

    function WordWrap(&$text, $maxwidth) {
        $text = trim($text);
        if ($text === '')
            return 0;
        $space = $this->GetStringWidth(' ');
        $lines = explode("\n", $text);
        $text = '';
        $count = 0;

        foreach ($lines as $line) {
            $words = preg_split('/ +/', $line);
            $width = 0;

            foreach ($words as $word) {
                $wordwidth = $this->GetStringWidth($word);
                if ($wordwidth > $maxwidth) {
                    // Word is too long, we cut it
                    for ($i = 0; $i < strlen($word); $i++) {
                        $wordwidth = $this->GetStringWidth(substr($word, $i, 1));
                        if ($width + $wordwidth <= $maxwidth) {
                            $width += $wordwidth;
                            $text .= substr($word, $i, 1);
                        } else {
                            $width = $wordwidth;
                            $text = rtrim($text) . "\n" . substr($word, $i, 1);
                            $count++;
                        }
                    }
                } elseif ($width + $wordwidth <= $maxwidth) {
                    $width += $wordwidth + $space;
                    $text .= $word . ' ';
                } else {
                    $width = $wordwidth + $space;
                    $text = rtrim($text) . "\n" . $word . ' ';
                    $count++;
                }
            }
            $text = rtrim($text) . "\n";
            $count++;
        }
        $text = rtrim($text);
        return $count;
    }

    //Entête de page 
    function Header() {
        $this->SetXY(26, 49);
        $this->SetFont('Times', 'BI', 11);
        $NomEmploye_etiquette = mb_convert_encoding("Employé", 'ISO-8859-1', 'UTF-8');
        $this->Cell(44, 5, $NomEmploye_etiquette, 0, 0, 'L');

        $this->SetXY(73, 49);
        $this->SetFont('Times', 'BI', 11);
        $NumEmploye_etiquette = mb_convert_encoding("Employé #", 'ISO-8859-1', 'UTF-8');
        $this->Cell(21, 5, $NumEmploye_etiquette, 0, 0, 'L');

        $this->SetXY(96, 49);
        $this->SetFont('Times', 'BI', 11);
        $Adresse1_etiquette = "Adresse";
        $this->Cell(70, 5, $Adresse1_etiquette, 0, 0, 'L');

        $this->SetXY(168, 49);
        $this->SetFont('Times', 'BI', 11);
        $Ville_etiquette = "Ville";
        $this->Cell(36, 5, $Ville_etiquette, 0, 0, 'L');

        $this->SetXY(206, 49);
        $this->SetFont('Times', 'BI', 11);
        $CodePostal_etiquette = "Code postal";
        $this->Cell(18, 5, $CodePostal_etiquette, 0, 0, 'L');

        // Ancien 216 -> 226
        $this->SetXY(226, 49);
        $this->SetFont('Times', 'BI', 11);
        $NumeroTel_etiquette = mb_convert_encoding("Téléphone", 'ISO-8859-1', 'UTF-8');
        $this->Cell(27, 5, $NumeroTel_etiquette, 0, 0, 'L');

        $this->SetLineWidth(1);
        $this->Line(25, 56, 243, 56);

        $this->SetXY(25, 26);
        $this->SetFont('Times', 'BI', 20);
        $etiquette16 = mb_convert_encoding("Liste des employés par employeur", 'ISO-8859-1', 'UTF-8');
        $this->Cell(119, 8, $etiquette16, 0, 0, 'L');

        $Image21 = 'logo/' . $_SESSION["syndicat"] . "." . $_SESSION['logo'];
        $this->Image($Image21, 197, 25, 47, 22);

        $this->SetXY(25, 37);
        $this->SetFont('Times', 'B', 14);
        $etiquette17 = "Nom de l'employeur : " . mb_convert_encoding($this->Nomemployeur, 'ISO-8859-1', 'UTF-8');
        $this->Cell(119, 8, $etiquette17, 0, 0, 'L');

        $this->SetY(58);
    }

    //Pied de page
    function Footer() {
        //Numéro de page
        //Positionnement à 1,5 cm du bas
        //Police Arial italique 8
        $this->SetY(-10);
        $this->SetFont('Arial', 'I', 8);
        $this->Cell(0, 10, 'Page ' . $this->PageNo() . '/{nb}', 0, 0, 'C');

        $this->SetLineWidth(0);
        $this->Line(26, 205, 244, 205);

        $etiquette43 = $this->configCentrale["syndicat_nom"];
        $etiquette45 = $this->configCentrale["adresse"] . ", " . $this->configCentrale["ville"] .
                " (" . $this->configCentrale["province"] . ") " . $this->configCentrale["codepostal"] .
                " Téléphone : " . str_replace(array("(", ")"), "", $this->configCentrale["telephone"]) .
                " Télécopieur : " . str_replace(array("(", ")"), "", $this->configCentrale["telecopieur"]);

        $this->SetXY(25, 201);
        $this->SetFont('Times', 'B', 9);
        $this->Cell(219, 4, mb_convert_encoding($etiquette43, 'ISO-8859-1', 'UTF-8'), 0, 0, 'C');

        $this->SetXY(25, 205);
        $this->SetFont('Times', 'B', 9);
        $this->Cell(219, 4, mb_convert_encoding($etiquette45, 'ISO-8859-1', 'UTF-8'), 0, 0, 'C');
    }
}

$i = 0;
unset($sqlparam);
$sqlparam["table"][] = "employes";
$sqlparam["table"][] = "employeurs";
$sqlparam["join"][] = "employes.refemployeur=employeurs.refemployeur";
$sqlparam["champs"][] = "CONCAT(employes.nomfamille,', ',employes.prenom) as nomemploye";
$sqlparam["champs"][] = "CONCAT(employes.adresse1, if(isnull(employes.adresse2),' ',"
        . "CONCAT(' ',employes.adresse2))) AS adresse";
$sqlparam["champs"][] = "employes.ville";
$sqlparam["champs"][] = "CONCAT(LEFT(employes.codepostal,3),\" \",RIGHT(employes.codepostal,3)) as codepost";
$sqlparam["champs"][] = "employes.numtel";
$sqlparam["champs"][] = "employes.numemploye";
$sqlparam["champs"][] = "employeurs.nomemployeur";
$sqlparam["ordre"][] = "CONCAT(employes.nomfamille, 's ', employes.prenom) ASC";

// Titre
$rapport_source = "rapport_employes_employeur";
require("lib/librapports.inc.php");

$result = executerRequeteSql($sqlparam);

// ****************************************************
// Vérification de la présence de détails dans la liste
// ****************************************************
if (!($result && is_array($result) && !empty($result))) {
    print"<center>Aucun employé pour cet employeur!";
    print "<br>";
    print "<input type='button' onclick='javascript:window.close();' value='Fermer'></center>";
    die();
}

//Instanciation de la classe dérivée
$pdf = new PDF('L', 'mm', 'Letter');

$pdf->Nomemployeur = $result[0]["nomemployeur"];

$pdf->SetMargins(25, 25);
$pdf->AliasNbPages();
$pdf->SetFont('Times', '', 9);
$pdf->AddPage();
$pdf->configCentrale = buildConfigCentrale();

foreach ($result as $clef => $champs) {

    $pdf->NomEmploye = mb_convert_encoding($champs["nomemploye"], 'ISO-8859-1', 'UTF-8'); //0
    $pdf->NumEmploye = mb_convert_encoding($champs["numemploye"], 'ISO-8859-1', 'UTF-8'); //1
    $pdf->Adresse = mb_convert_encoding($champs["adresse"], 'ISO-8859-1', 'UTF-8'); //2
    $pdf->Ville = mb_convert_encoding(substr($champs["ville"], 0, 20), 'ISO-8859-1', 'UTF-8'); //3
    $pdf->CodePostal = mb_convert_encoding($champs["codepost"], 'ISO-8859-1', 'UTF-8'); //4
    $pdf->NumeroTel = mb_convert_encoding(formatPhone($champs["numtel"]), 'ISO-8859-1', 'UTF-8'); //5

    $pdf->SetX(26);
    $pdf->SetFont('Times', '', 8);
    $pdf->Cell(44, 4, $pdf->NomEmploye, 0, 0, 'L');

    $pdf->SetX(72);
    $pdf->SetFont('Times', '', 8);
    $pdf->Cell(21, 4, $pdf->NumEmploye, 0, 0, 'L');

    $pdf->SetX(95);
    $pdf->SetFont('Times', '', 8);
    $pdf->Cell(52, 4, $pdf->Adresse, 0, 0, 'L');

    $pdf->SetX(167);
    $pdf->SetFont('Times', '', 8);
    $pdf->Cell(36, 4, $pdf->Ville, 0, 0, 'L');

    $pdf->SetX(205);
    $pdf->SetFont('Times', '', 8);
    $pdf->Cell(18, 4, $pdf->CodePostal, 0, 0, 'L');

    $pdf->SetX(225);
    $pdf->SetFont('Times', '', 8);
    $pdf->Cell(27, 4, $pdf->NumeroTel, 0, 0, 'L');

    $pdf->Ln();
}

$pdf->Output();
